package nhl_stats;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import jxl.*;
import jxl.read.biff.BiffException;

/**
 *
 * @author richi_000
 */
public class statsReader {
    
    private ArrayList<Player> playerStats;
    
    public statsReader() {
        //constructor
        
        playerStats = new ArrayList<>();
    }
    
    public void loadSheets() {
        try {
            File file = new File("data");
            
            File[] dataFiles = file.listFiles();
            
            //TODO load in whole database of years
            
            for(int j = 0; j < dataFiles.length; j++) {
            
                if(dataFiles[j].getName().contains(".xls")) {
                
                    Workbook book = Workbook.getWorkbook(new File(dataFiles[j].getPath()));
                    System.out.println(dataFiles[j].getPath());

                    Sheet sheet = book.getSheet(0);
                    int counter = 0;
                    System.out.println("Rows " + sheet.getRows());
                    
                    data tempdata;
                    
                    for(int i = 1; i < sheet.getRows();i++) {
                        if(dataFiles[j].getName().contains("2012-13")) {
                            tempdata = do20122013(sheet, i);
                        } else if(dataFiles[j].getName().contains("2013-14")) {
                            tempdata = do20132014(sheet, i, book.getSheet(2), book.getSheet(3));
                            if(tempdata == null) {
                                System.err.println("End of player reached");
                            }
                            //check if playerstats has a same named player.
                        } else {
                            tempdata = get19982011(sheet,i);
                        }
                        
                        if(tempdata != null) {
                            String First, Last;
                            
                            First = tempdata.getfName();
                            Last = tempdata.getlName();
                            boolean playerFound = false;                            
                            
                            for(Player player: playerStats) {
                                if(player.getFname().contains(First.trim()) && player.getLname().contains(Last)) { //same player
                                    player.addYear(tempdata);
                                    playerFound = true;
                                    System.out.println("Player added to existing data");
                                    break;
                                } 
                            }
                            
                            if(!playerFound) {
                                playerStats.add(new Player(tempdata)); //didn't find player so add another with new data
                                System.out.println("New Player Data Created");
                            }
                        }
                    }
                } else {
                    System.err.println("File isn't an excel spreadsheet");
                }
            }
            
        } catch (IOException | BiffException ex) {
            System.err.println("Error");
            System.exit(0);
        }       
    }
    
    public Player findPlayer(String searchItem) {
        
        for(Player player:playerStats) {
            if(player.getFname().contains(searchItem) || player.getLname().contains(searchItem))
                return player;
        }
        System.err.println("Could not find player with specified name");
        return null;
    }
    
    public ArrayList<Player> getPlayers() {
        return playerStats;
    }
    
    public data do20132014(Sheet sheet, int i, Sheet ppsheet, Sheet pksheet) {    
        String fname, lname, team, position;
        int GP, G, A, P, SOG, PM, PIM;
        double TOI, PPTOI;
        int PPG, SHG;

        //Found player's last name *hopefully* now load everything we need
        try {
            LabelCell lcell = (LabelCell) sheet.getCell(15, i);
            lname = lcell.getString();

            lcell = (LabelCell) sheet.getCell(14, i);
            fname = lcell.getString();
            
            System.out.println(fname + " " + lname);

            //loaded name now load team and position
            lcell = (LabelCell) sheet.getCell(13, i);
            team = lcell.getString();
            lcell = (LabelCell) sheet.getCell(19, i);
            if (lcell.getString().contains("D") || lcell.getString().contains("d")) {
                position = "D";
            } else {
                position = "F";
            }

            //load in GP, G, A, P, SOG, PM, PPG, SHG, PIM
            NumberCell ncell = (NumberCell) sheet.getCell(20, i);
            GP = (int) ncell.getValue();
            ncell = (NumberCell) sheet.getCell(21, i);
            G = (int) ncell.getValue();
            ncell = (NumberCell) sheet.getCell(22, i);
            A = (int) ncell.getValue();
            ncell = (NumberCell) sheet.getCell(23, i);
            P = (int) ncell.getValue();
            ncell = (NumberCell) sheet.getCell(24, i);
            PM = (int) ncell.getValue();
            ncell = (NumberCell) sheet.getCell(25, i);
            SOG = (int) ncell.getValue();

            ncell = (NumberCell) sheet.getCell(60, i);
            PIM = (int) ncell.getValue();

            ncell = (NumberCell) sheet.getCell(35, i);
            TOI = ncell.getValue();

            String ppfname, pplname;
            lcell = (LabelCell) ppsheet.getCell(0,i);
            ppfname = lcell.getContents();
            lcell = (LabelCell) ppsheet.getCell(1, i);
            pplname = lcell.getContents();

            if(fname.contains(ppfname) && lname.contains(pplname)) {
                ncell = (NumberCell) ppsheet.getCell(6, i); 
                PPG = (int) ncell.getValue();
                ncell = (NumberCell) ppsheet.getCell(24, i); 
                PPTOI = ncell.getValue();
            } else {
                PPG = 0;
                PPTOI = 0.0f;
            }

            lcell = (LabelCell) ppsheet.getCell(0,i);
            ppfname = lcell.getContents();
            lcell = (LabelCell) ppsheet.getCell(1, i);
            pplname = lcell.getContents();

            if(fname.contains(ppfname) && lname.contains(pplname)) {
                ncell = (NumberCell) pksheet.getCell(6, i); 
                SHG = (int) ncell.getValue();
            } else {
                SHG = 0;
            }    
            return new data(fname, lname, team, position, GP, G, A, P, PM, SOG, TOI, PIM, PPTOI, PPG, SHG, 2014);
        } catch(Exception e) {
            return null;
        }
    } 
    
    public data do20122013(Sheet sheet, int i) {
        String fname, lname, team, position;
        int GP, G, A, P, SOG, PM, PIM;
        double TOI, PPTOI;
        int PPG, SHG;

        //Found player's last name *hopefully* now load everything we need
        LabelCell lcell = (LabelCell) sheet.getCell(11, i);
        lname = lcell.getString();

        lcell = (LabelCell) sheet.getCell(12, i);
        fname = lcell.getString();
        System.out.println(fname + " " + lname);

        //loaded name now load team and position
        lcell = (LabelCell) sheet.getCell(13, i);
        team = lcell.getString();
        lcell = (LabelCell) sheet.getCell(15, i);
        if (lcell.getString().contains("D") || lcell.getString().contains("d")) {
            position = "D";
        } else {
            position = "F";
        }

        //load in GP, G, A, P, SOG, PM, PPG, SHG, PIM
        NumberCell ncell = (NumberCell) sheet.getCell(16, i);
        GP = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(17, i);
        G = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(18, i);
        A = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(19, i);
        P = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(20, i);
        PM = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(21, i);
        SOG = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(140, i);
        PPG = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(169, i);
        SHG = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(48, i);
        PIM = (int) ncell.getValue();

        //TOI and PPTOI 
        ncell = (NumberCell) sheet.getCell(24, i);
        TOI = ncell.getValue();
        ncell = (NumberCell) sheet.getCell(136, i);
        PPTOI = ncell.getValue();
        
        return new data(fname, lname, team, position, GP, G, A, P, PM, SOG, TOI, PIM, PPTOI, PPG, SHG, 2013);
    }
    
    public data get19982011(Sheet sheet, int i) {
        String fname, lname, team, position;
        int GP, G, A, P, SOG, PM, PIM;
        double TOI, PPTOI;
        int PPG, SHG;
        int year;

        //Load in name and then split the string
        LabelCell lcell = (LabelCell) sheet.getCell(9, i);
        String name = lcell.getString();
        String parts[] = name.split(" ");
        fname = parts[0];
        lname = parts[1];
        if(parts.length > 2) {
            lname += parts[2];
        }
        
        System.out.println(fname + " " + lname);

        //loaded name now load team and position
        lcell = (LabelCell) sheet.getCell(10, i);
        team = lcell.getString();
        lcell = (LabelCell) sheet.getCell(11, i);
        if (lcell.getString().contains("D") || lcell.getString().contains("d")) {
            position = "D";
        } else {
            position = "F";
        }
                
        //load in GP, G, A, P, SOG, PM, PPG, SHG, PIM
        NumberCell ncell = (NumberCell) sheet.getCell(13, i);
        GP = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(14, i);
        G = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(15, i);
        A = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(16, i);
        P = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(17, i);
        PM = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(24, i);
        SOG = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(35, i);
        PPG = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(39, i);
        SHG = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(18, i);
        PIM = (int) ncell.getValue();
        ncell = (NumberCell) sheet.getCell(12, i);
        year = (int) ncell.getValue();

        //TOI and PPTOI 
        if(GP != 0) {
            ncell = (NumberCell) sheet.getCell(27, i);
            TOI = ncell.getValue();
            ncell = (NumberCell) sheet.getCell(38, i);
            PPTOI = ncell.getValue();
        } else {
            TOI = 0.0f;
            PPTOI = 0.0f;
        }
        
        return new data(fname, lname, team, position, GP, G, A, P, PM, SOG, TOI, PIM, PPTOI, PPG, SHG, year);
    }
}
